<!--头部公共部分-->
{include file="common/header" /}

<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>首页</a>
        <a>教务</a>
        <a>
          <cite>排课管理</cite></a>
      </span>
    <a class="layui-btn layui-btn-xs layui-btn-small" style="float:right;margin-top: 10px;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh"></i> </a>
</div>
<div id="dm_arrang" class="x-body" class="clear:both;">
    <div class="layui-row"
         style="padding-left:20px;border-left:1px solid #e5e5e5;border-right:1px solid #e5e5e5;">
        <div class="layui-form layui-col-md12 x-so layui-form-pane">

            <div class="layui-input-inline">
                <select name="campus_id" lay-verify="required" lay-search="">
                    <option value="">选择校区</option>
                    {volist name="campus" id="campu"}
                    <option value="{$campu.id}">{$campu.campus_name}</option>
                    {/volist}
                </select>
            </div>

            <div class="layui-input-inline">
                <!--
                <input type="text" name="class_grades_name" placeholder="班级名称" autocomplete="off" class="layui-input">
                -->
                <select name="class_grades_name" lay-search="">
                    <option value="">请输入班级名称</option>
                    {volist name="grade" id="vo"}
                    <option value="{$vo.grades_name}">{$vo.grades_name}</option>
                    {/volist}
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="course_id" lay-search="">
                    <option value="">请输入课程名称</option>
                    {volist name="course_list" id="vo"}
                    <option value="{$vo.id}">{$vo.course_name}</option>
                    {/volist}
                </select>
            </div>

            <div class="layui-input-inline">
                <input type="text" class="layui-input" style="width: auto;" placeholder="开课日期" name="class_date" id="start">
            </div>

            <div class="layui-input-inline">
                <button class="layui-btn" id="search">
                    <i class="layui-icon layui-icon-search"></i>筛选</button>
                <button class="layui-btn layui-btn-danger" id="clear">
                    <i class="layui-icon"></i>重置</button>
                <!--
                <button class="layui-btn" id="table-add">
                    <i class="layui-icon layui-icon-add-circle-fine"></i>新增排课</button>
                    -->
            </div>
        </div>
    </div>
    <table class="layui-table" id="datalist" lay-filter="Event"></table>
    <script type="text/html" id="name">
        <a lay-event="see_info" class="layui-table-link">{{ d.class_grades_name }}</a>
    </script>
    <script type="text/html" id="barTable">
        {{# if(d.status == 1){ }}
        <a class="layui-btn layui-btn-xs " lay-event="cancel_dm">撤销上课</a>
        {{# } else { }}
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="dm">点名上课</a>
        {{# } }}



        {{# if(d.status == 1){ }}
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="modify_student">修改上课信息</a>
        {{# } else { }}
        <a class="layui-btn layui-btn-xs layui-btn-disabled"><span title="该课节还未【点名上课】无上课信息">修改上课信息<sup><span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span></sup></span></a>
        {{# } }}


        {{# if(d.status == 1){ }}
        <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="change_content">修改上课内容</a>
        {{# } else { }}
        <a class="layui-btn layui-btn-xs layui-btn-disabled"><span title="该课节还未【点名上课】无上课内容">修改上课内容<sup><span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span></sup></span></a>
        {{# } }}


        {{# if(d.status == 1){ }}
        <a class="layui-btn layui-btn-xs layui-btn-disabled" ><span title="已上课班级无法操作【删除】功能">删除<sup><span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span></sup></span></a>
        {{# } else { }}
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
        {{# } }}




    </script>
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>
    layui.use(['table', 'form','element', 'laydate', 'layer'], function () {
        var table = layui.table,
            laydate = layui.laydate,
            form = layui.form,
            element = layui.element,
            layer = layui.layer,
            $ = layui.$;

        var loading = layer.load();
        form.render(null, 'form-organization');
        form.on('submit(form-organization-search)', function (data) {
            let searchLoading = layer.load();
            table.reload('table-organization', {
                url: "{:url('Arranging/lists')}"
                , where: data.field
                , done: function () {
                    layer.close(searchLoading);
                }
            });
            return false;
        });

        $('#table-add').click(function () {
            var url = "{:url('Arranging/add')}";
            location.href = url;
        });

        //日期
        laydate.render({
            elem: '#start',
            range:true
            ,trigger: 'click'
        });

        //方法级渲染
        var tableIns = table.render({
            elem: '#datalist'
            , url: '{:url("Arranging/lists")}'
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
            if (res.code === 1) {
                res.code = 0;
            } else {
                res.code = -1;
            }
            return {
                "code": res.code, //解析接口状态
                "msg": res.message, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.data //解析数据列表
            };
            },
            cols: [[
                {field: 'id', title: 'ID', width: 100, align: 'center'
                }, {field: 'course_name', style:'color:#01AAED;font-weight: bold;',title: '课程名称', width: 300, align: 'center'
                }, {field: 'class_grades_name', title: '班级名称', width: 400, align: 'center'
                }, {field: 'class_date', title: '开课日期', width: 120, align: 'center'
                }, {field: 'campus_name', title: '归属校区', width: 100, align: 'center'
                }, {field: 'class_room', title: '教室', width: 100, align: 'center'
                }, {field: 'weekday', title: '星期', width: 100, align: 'center'
                }, {field: 'teacher', title: '上课老师', width: 100, align: 'center'
                }, {field: 'headmaster', title: '班主任', width: 100, align: 'center'
                }, {field: 'assistant', title: '助教', width: 100, align: 'center'
                }, {field: 'class_subject_start', title: '开课时间', width: 120, align: 'center'
                }, {field: 'class_subject_end', title: '下课时间', width: 120, align: 'center'
                }, {field: 'class_length', title: '上课时长', width: 100, align: 'center'
                }, {field: 'actual_number', title: '实到人数?', width: 100, align: 'center'
                }, {field: 'status_text', title: '是否上课?', width: 100, align: 'center'
                }, {fixed: 'right', title: '操作', width: 360, align: 'center', toolbar: '#barTable'}
            ]]
            , height: 'full-180'
            , page: true
            , done: function () {
                $("[data-field='id']").css('display','none');
                layer.close(loading);
             }
        });

        //监听工具条
        table.on('tool(Event)', function (obj) {
            var data = obj.data; //获得当前行数据
            console.log(data)
            var layEvent = obj.event; //获得 lay-event 对应的值
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if (layEvent === 'dm') { //查看
                var title = '点名上课';
                if(data.class_grades_name.indexOf("补课班级") != -1){
                    title = '点名上课【补课】';
                }
                layer.open({
                    skin: 'layui-layer-lan',
                    type: 2,
                    closeBtn: 2,
                    title: title,
                    area: ['98%', '95%'],
                    shadeClose: false,
                    content: "{:url('Arranging/roll_call')}?id="+data.id,
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        body.find("input[name='grades_name']").val(data.class_grades_name);
                        body.find("input[name='class_time']").val(data.class_date);
                    },
                    cancel: function(index, layero){
                        var field = {};
                        field.id = data.id;
                        $.ajax({
                            url: '/admin/arranging/close',
                            method: "post",
                            data: field,
                            beforeSend: function () {
                                isLoad = layer.load(0, {
                                    shade: false
                                }); //0代表加载的风格，支持0-2
                            },
                            success: function (result) {
                                layer.msg(result.msg);
                                if (result.code === 1) {
                                    layer.close(index)
                                    tableIns.reload();

                                }

                            },
                            complete: function () {
                                layer.close(isLoad)
                            },
                            error: function (error) {
                                console.log(error)
                            }
                        });
                        return false;
                    },

                    end:function () {

                        tableIns.reload();
                    }

                });
            }else if(layEvent === 'del'){
                layer.confirm('真的删除该节课么?', function (index) {
                    $.post("{:url('Arranging/class_del')}", {
                        id: data.id
                    }, function (result) {
                        if (result.code === 1) {
                            layer.msg(result.msg, {
                                time: 2000
                            });
                             tableIns.reload();

                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }, "json")
                });

            }else if(layEvent === 'change_content'){  //修改上课内容

                layer.open({
                    skin: 'layui-layer-lan',
                    type: 2,
                    closeBtn: 2,
                    title: '修改上课内容',
                    area: ['50%', '65%'],
                    shadeClose: false,
                    content: "{:url('Arranging/get_content')}?id="+data.id
                });

            }else if(layEvent === 'cancel_dm'){     //撤消排课
                layer.confirm('真的撤销该节课么?', function (index) {
                    $.post("{:url('Arranging/cancel_grades')}", {
                        id: data.id
                    }, function (result) {
                        if (result.code === 1) {
                                layer.msg(result.msg, {
                                    time: 2000
                                });
                            tableIns.reload();

                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 2000
                            });
                        }
                    }, "json")
                });
            }else if(layEvent === 'see_info'){
                //修改上课信息

            }else if(layEvent === 'modify_student'){

                layer.open({
                    skin: 'layui-layer-lan',
                    type: 2,
                    closeBtn: 2,
                    title: '修改上课信息',
                    area: ['98%', '95%'],
                    shadeClose: false,
                    content: "{:url('Arranging/modify_student')}?id="+data.id,
                    success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        body.find("input[name='grades_name']").val(data.class_grades_name);
                        body.find("input[name='class_time']").val(data.class_date);
                    },
                    cancel: function(index, layero){

                    },

                    end:function () {

                        tableIns.reload();
                    }

                });

            }
        });

        form.on('switch(sexDemo)', function(obj){
            console.log(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis)
            var status;
            if (obj.elem.checked == true) {
                status = 0;
            } else {
                status = 1;
            }
            //激活与禁用状态改变
            $.post("{:url('Arranging/arranging_check_status')}", {
                id: this.value,
                status: status
            }, function (rJson) {
                if (rJson.code === 0) {
                    layer.msg(rJson.msg, {
                        time: 2000
                    });
                    setTimeout(function () {
                        location.reload();
                    }, 2000)
                } else {
                    layer.msg(rJson.msg, {
                        icon: 2,
                        time: 2000
                    });
                }
            }, "json")
        });

        //搜索项
        $('#search').click(function () {
            var field = {};
            field.class_grades_name = $('select[name=class_grades_name]').val();
            field.course_id = $('select[name=course_id]').val();
            field.campus_id = $('select[name=campus_id] option:selected').val();

            var create_date_rang = $('input[name=class_date]').val();
            if(create_date_rang.length > 0){
                strs_date=create_date_rang.split("-"); //字符分割
                field.start_date = strs_date[0]+'-'+strs_date[1]+'-'+strs_date[2];
                field.end_date = strs_date[3]+'-'+strs_date[4]+'-'+strs_date[5];
            }

            console.log(field);
            tableIns.reload({
                where: field,
                method: 'POST',
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
        //清除
        $('#clear').click(function () {
            $('.layui-form-pane').find('.layui-input-inline').each(function (index, element) {
                $(element).find('input[type=text]').val('');
                $(element).find('select').val('');
            })
            tableIns.reload({
                where: {
                    'class_grades_name': '',
                    'course_id': '',
                    'campus_id':'',
                    'class_date':'',
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
    });
</script>

